home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Best of MacTutor - S…e Code for Volumes 1 to 5
/
The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin
/
Source Code
/
#02 (Apr85-Jul85)
/
modula 2
/
modula Vol. 1 #6
/
HanoiPuzzle.MOD
next >
Wrap
Text File
|
1985-02-26
|
1KB
|
56 lines
MODULE HanoiPuzzle;
(* Declare I/O from Modula-2 Standard Library *)
FROM Terminal IMPORT ClearScreen;
FROM InOut IMPORT WriteLn, WriteString, WriteCard, ReadCard,
Write;
CONST Start = "a";
Int = "b";
Finish = "c";
VAR DiskCount: CARDINAL; Done: BOOLEAN;
(* Get number of disks or set terminate flag *)
PROCEDURE GetInput (VAR NumberOfDisks: CARDINAL;
VAR Quit: BOOLEAN);
BEGIN
ClearScreen;
WriteString("Enter number of disks (between 3 and 9)");
WriteLn;
WriteString("To quit - enter number out of range");
WriteLn;
ReadCard(NumberOfDisks);
IF (NumberOfDisks < 3) OR (NumberOfDisks > 9)
THEN Quit := TRUE
ELSE Quit := FALSE
END; (*IF*)
ClearScreen;
END GetInput;
(* The recursive guts of the programs ... calculate moves. *)
PROCEDURE Hanoi(n: CARDINAL; StartNeedle, IntNeedle,
FinishNeedle: CHAR);
BEGIN
IF n#0
THEN
Hanoi(n-1, StartNeedle, FinishNeedle, IntNeedle);
WriteLn;
WriteString("Move disk -");
WriteCard(n,2);
WriteString(" from ");
Write(StartNeedle);
WriteString(" to ");
Write(FinishNeedle);
Hanoi(n-1, IntNeedle, StartNeedle, FinishNeedle);
END; (*IF*)
END Hanoi;
(* Mainline ... control main loop ... get input & do it. *)
BEGIN
GetInput(DiskCount, Done);
WHILE NOT Done DO
Hanoi(DiskCount, Start, Int, Finish);
GetInput(DiskCount, Done);
END; (*WHILE*)
END HanoiPuzzle.